﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;
using System.Web.UI;

namespace LB.SharePoint.XSLTListViewRenderer.XSLTListViewPart
{
    public class XSLTEditorPart : EditorPart
    {
        private DropDownList spListDropDown = new DropDownList();
        private DropDownList spViewDropDown = new DropDownList();

        public override bool ApplyChanges()
        {
            XSLTListViewPart myWebPart = WebPartToEdit as XSLTListViewPart;
            if (myWebPart != null)
            {
                myWebPart.ListGuid = spListDropDown.SelectedValue;
                myWebPart.ViewGuid = spViewDropDown.SelectedValue;
            }
            return true; 
        }

        public override void SyncChanges()
        {
            XSLTListViewPart myWebPart = WebPartToEdit as XSLTListViewPart;
            if(!String.IsNullOrEmpty(myWebPart.ListGuid))
            {
                try
                {
                    if (spListDropDown.Items.Count == 0)
                    {
                        AddSPListItems();
                    }
                    spListDropDown.SelectedValue = myWebPart.ListGuid;
                    AddSPViewItems();
                    if (!String.IsNullOrEmpty(myWebPart.ViewGuid))
                    {
                        spViewDropDown.SelectedValue = myWebPart.ViewGuid;
                    }
                }
                catch { }
            }
        }

        protected override void CreateChildControls()
        {
            this.Title = "List Settings";
            this.Controls.Add(BuildControlTree());
            XSLTListViewPart myWebPart = WebPartToEdit as XSLTListViewPart;
            if (myWebPart != null)
            {
                spListDropDown.SelectedValue = myWebPart.ListGuid;
            }
        }

        private Control BuildControlTree()
        {
            Panel genericPanel = new Panel() { CssClass = "UserGeneric" };
            Panel headPanel = new Panel() { CssClass = "UserSectionHead ms-bold" };
            headPanel.Controls.Add(new Label()
            {
                Text = "Lists",
                AssociatedControlID = spListDropDown.ID
            });
            genericPanel.Controls.Add(headPanel);

            Panel bodyPanel = new Panel()
            {
                CssClass = "UserSectionBody"
            };
            genericPanel.Controls.Add(bodyPanel);
            Panel groupPanel = new Panel()
            {
                CssClass = "UserControlGroup"
            };
            bodyPanel.Controls.Add(groupPanel);
            if (spListDropDown.Items.Count == 0)
            {
                AddSPListItems();
            }
            spListDropDown.AutoPostBack = true;
            spListDropDown.CssClass = "UserSelect";
            spListDropDown.Width = Unit.Pixel(176);
            spListDropDown.SelectedIndexChanged += new EventHandler(spListDropDown_SelectedIndexChanged);
            groupPanel.Controls.Add(spListDropDown);
            groupPanel.Controls.Add(new LiteralControl("<br/>"));
            genericPanel.Controls.Add(new Panel() { CssClass = "UserDottedLine" });
            headPanel = new Panel()
            {
                CssClass = "UserSectionHead ms-bold"
            };
            headPanel.Controls.Add(new Label()
            {
                Text="Selected List Views",
                AssociatedControlID = spViewDropDown.ID
            });
            genericPanel.Controls.Add(headPanel);
            bodyPanel = new Panel()
            {
                CssClass = "UserSectionBody"
            };
            genericPanel.Controls.Add(bodyPanel);
            groupPanel = new Panel()
            {
                CssClass = "UserControlGroup"
            };
            spViewDropDown.Enabled = false;
            groupPanel.Controls.Add(spViewDropDown);
            bodyPanel.Controls.Add(groupPanel);

            return genericPanel;
        }

        void spListDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            AddSPViewItems();
            spViewDropDown.Enabled = true;
        }

        protected void AddSPViewItems()
        {
            spViewDropDown.Items.Clear();

            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists[new Guid(spListDropDown.SelectedValue)];
            foreach (SPView view in list.Views)
            {
                if (!view.Hidden)
                {
                    spViewDropDown.Items.Add(new ListItem { Text = view.Title, Value = view.ID.ToString() });
                }
            }

            spViewDropDown.Enabled = true;
        }

        protected void AddSPListItems()
        {
            SPWeb web = SPContext.Current.Web;
            foreach (SPList list in web.Lists)
            {
                if (!list.Hidden)
                {
                    spListDropDown.Items.Add(new ListItem { Text = list.Title, Value = list.ID.ToString() });
                }
            }
        }
    }
}
